import 'package:bitcoin_mine/services/in_app_purchase_service.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import '../common/global.dart';
import '../model/mine_info.dart';

class SubscriptionController extends GetxController {
  // 订阅层级选择
  final RxInt selectedTier =
      0.obs; // 0: BASIC, 1: MODERATE, 2: ADVANCED, 3: TOP-TIER

  // 订阅计划选择
  final RxInt selectedPlan = 0.obs; // 0: 第一个计划, 1: 第二个计划, 2: 第三个计划

  // 订阅层级列表
  final List<String> subscriptionTiers = [
    'BASIC',
    'MODERATE',
    'ADVANCED',
    'TOP-TIER',
  ];

  late InAppPurchaseService inAppPurchaseService;

  @override
  void onInit() {
    super.onInit();
    inAppPurchaseService = Get.find<InAppPurchaseService>();
  }

  // 选择订阅层级
  void selectTier(int index) {
    selectedTier.value = index;
    // 重置选择的计划为第一个
    selectedPlan.value = 0;
  }

  // 选择订阅计划
  void selectPlan(int index) {
    selectedPlan.value = index;
  }

  // 恢复购买
  void restorePurchases() {
    print('restore purchases...');
    Fluttertoast.showToast(msg: 'restore purchases...');
    inAppPurchaseService.restorePurchases();
    // Get.snackbar('提示', '正在恢复购买...', snackPosition: SnackPosition.BOTTOM);
  }

  // 订阅
  void subscribe() {
    inAppPurchaseService.purchaseProduct(getCurrentProduct());
  }

  /// 获取当前选择的产品
  ProductDetails? getCurrentProduct() {
    return inAppPurchaseService.getCurrentProduct(
      selectedTier.value,
      selectedPlan.value,
    );
  }

  // 从 mine_info 中获取当前选中的 Good
  Good? get _currentGood {
    final MineInfo? info = Global.mineInfo.value;
    if (info == null) return null;
    final List<Good> goods = _goodsByTier(info, selectedTier.value);
    if (goods.isEmpty || selectedPlan.value >= goods.length) return null;
    final Good selectedGood = goods[selectedPlan.value];
    final Good good = Global.goods.firstWhere(
      (element) => element.product_id == selectedGood.product_id,
    );

    return good;
  }

  // 获取当前选择的价格
  String get currentSelectedPrice {
    final Good? g = _currentGood;
    if (g == null) return '\$0.00/1 month';
    final ProductDetails? productInfo = inAppPurchaseService.getProductInfo(
      g.product_id,
    );
    return '${g.discount_price != null ? '${productInfo?.currencySymbol}${g.discount_price}' : '${productInfo?.price}'}/${_periodText(g.circle)}';
  }

  String get currentSelectedNextPrice {
    final Good? g = _currentGood;
    if (g == null) return '\$0.00/1 month';
    final ProductDetails? productInfo = inAppPurchaseService.getProductInfo(
      g.product_id,
    );
    return 'The next: ${productInfo?.price}/${_periodText(g.circle)}';
  }

  // 获取当前选择的详情
  String get currentSelectedDetails {
    // 目前 UI 固定描述
    return '+ Continuous improvement\nMining speed';
  }

  // 获取当前选择的挖矿速度
  String get currentSelectedSpeed {
    final Good? g = _currentGood;
    if (g == null) return '0 Gh/s';
    return '${g.hashrate.toStringAsFixed(2)} Gh/s';
  }

  List<Good> _goodsByTier(MineInfo info, int tier) {
    switch (tier) {
      case 0:
        return info.goods_map.group1;
      case 1:
        return info.goods_map.group2;
      case 2:
        return info.goods_map.group3;
      case 3:
        return info.goods_map.group4;
      default:
        return info.goods_map.group1;
    }
  }

  String _periodText(int circle) {
    switch (circle) {
      case 1:
        return 'week';
      case 2:
        return 'month';
      case 3:
        return 'season';
      case 4:
        return 'year';
      default:
        return 'month';
    }
  }

  // 返回上一页
  void goBack() {
    Get.back();
  }
}
